<?php
/**
 * Description: 好物轻测-确定抽奖名单
 * Created by PhpStorm.
 * User: 玉坤
 * Date: 2021/6/15
 * Time: 14:04
 */

use Biz\model\brandActivity\BrandActivityBasic;
use Biz\model\GoodsTestActivity\FineGoodsJoinModel;
use Biz\model\GoodsTestActivity\FineGoodsJoinRecordModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\model\GoodsTestActivity\FineGoodsMsgModel;
use Biz\service\brand\BrandActivity;
use Doraemon\model\Sysmsg;
use Doraemon\service\User;
use Doraemon\tools\LinkFormat;

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED);
ini_set('memory_limit', '1024M');
require_once '/data/wwwroot/g_vendor/autoload.php';

$start = microtime(true);
$page = 1;
$pageSize = 50;
$lists = [];
$all_num = 0;

try {
    do {
        $limit = [($page - 1) * $pageSize, $pageSize];
        $where = [
            "LIMIT" => $limit,
            'activity_state' => FineGoodsModel::STATE_PASS,
            'type[<]' => FineGoodsModel::TYPE_EVALUATION_OLD, //这里只查询 轻测、开箱、测评类型的活动
            'report_num' => 0,
            'schedule_end_time[<]' => strtotime(date("Y-m-d")) - FineGoodsModel::VERIFY_USER_TIME,
            'schedule_end_time[>=]' => strtotime(date("Y-m-d")) - FineGoodsModel::VERIFY_USER_TIME - 86400,
        ];
        $lists = FineGoodsModel::select(['id', 'brand_name', 'activity_title','type'], $where);
        if (!$lists) {
            break;
        }
        $info = [];
        foreach ($lists as $list) {
            $join_users = FineGoodsJoinModel::getLastPersons($list['id'], ['fine_goods_id', 'uid']);
            if (!$join_users) {
                continue;
            }
            \Biz\service\GoodsTestActivity\FineGoodsService::addMsgRecord(FineGoodsMsgModel::MSG_SUCCESS, $join_users);
            foreach ($join_users as $join_user) {
                //推送系统消息
                pushMsg($list['brand_name'], $list['activity_title'], $join_user['uid'], $list['id'] , $list['type']);
            }
        }
        $page++;
    } while ($lists);
} catch (\Exception $e) {
    \Doraemon\tools\Log::error('好物轻测-名单公布_error:' . $e->getMessage(), $lists);
}
$end = microtime(true);
$time = $end - $start;
$second = round($time, 3);
var_dump('完成时间：' . date('Y-m-d H:i:s') . '   总耗时:' . $second . '秒');


function pushMsg($brand_name, $activity_title, $uid, $id,$type)
{
    $recordInfo = FineGoodsJoinRecordModel::first(['uid' => $uid, 'fine_goods_id' => $id, 'dynamic_sync' => FineGoodsJoinRecordModel::SHARE]);
    if ($recordInfo) {
        BrandActivity::createDynamic($uid, $id, BrandActivityBasic::ACTIVITY_TYPE_LIGHT_TEST);
    }
    $title = '恭喜你成为好物体验官！';
    $activityTypeName = FineGoodsModel::TYPE_MAP[$type];
    if($brand_name){
        $alert = 'hi，恭喜你成为「' . $brand_name . ' | ' . $activity_title . "」产品体验官。{$activityTypeName}产品将于3个工作日内发出，请注意查收。点击查看活动详情及{$activityTypeName}注意事项。";
    }else{
        $alert = 'hi，恭喜你成为「' . $activity_title . "」产品体验官。{$activityTypeName}产品将于3个工作日内发出，请注意查收。点击查看活动详情及{$activityTypeName}注意事项。";
    }
    $link = 'https://m.haohaozhu.cn/brand/light-test/detail?id=' . $id;
    $link = LinkFormat::encryptionLink($link);
    if ($uid > 0) {
        $data = [
            'msgCode' => 0,
            'to_uid' => $uid,
            'msgParams' => [
                'from_uid' => User::HAOHAOZHUFULIGUAN_UID,
                'title' => $title,
                'detail' => $alert,
                'img' => 'https://img.haohaozhu.cn/Biz-imageShow/o_phone/6e8/4b36820ku0af05B00qwhg7i',
                'link' => $link,
                'type' => Sysmsg::TYPE_SYS_MSG,
            ],
        ];
        var_dump($data);
        \Doraemon\cache\Queue::insertQueue('queue/Sysmsg/Send', $data, \Doraemon\cache\Queue::QUEUE_DORAEMON);
    }
}